﻿namespace Microsoft.AspNetCore.App
{
    /// <summary>
    /// 简化中间件的注册
    /// </summary>
    public static class UseExtensions
    {
        /// <summary>
        /// 注册一个中间件
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="middleware"></param>
        public static void Use(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
        {
            builder.Use(next =>
            {
                return async context =>
                {
                    await middleware(context, () => next(context));
                };
            });
        }

        /// <summary>
        /// 注册一个结束点
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="middleware"></param>
        public static void Run(this IApplicationBuilder builder, RequestDelegate middleware)
        {
            builder.Use(_ =>
            {
                return async context =>
                {
                    await middleware(context);
                };
            });
        }
    }
}
